import urllib.request as ur
import urllib.parse as up
import json


def fanyi():
    ff = input('请输入待翻译内容：')
    data = {
        'kw': ff
    }
    datas = up.urlencode(data)
    request = ur.Request('https://fanyi.baidu.com/sug', data=datas.encode(encoding='utf-8'))
    response = ur.urlopen(request).read()
    rere = json.loads(response)
    try:
        print(rere['data'][0]['v'])
    except:
        print('请输入正确的文本。')
        fanyi()


if __name__ == '__main__':
    fanyi()
